package com.ithong.crm.controller;

import com.alibaba.fastjson.JSON;
import com.ithong.crm.model.CourseOrder;
import com.ithong.crm.model.PageResult;
import com.ithong.crm.service.CourseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CourseOrderController {

    @Autowired
    CourseOrderService courseOrderService;

    @GetMapping("/list")
    public String list(){
        return "courseorder/list";
    }

    /**
     *
     * @param page 显示的当前页
     * @param limit 每次显示多少条
     * @return
     */
    @GetMapping("/listJson")
    @ResponseBody
    public PageResult<CourseOrder> listJson(CourseOrder condition,int page,int limit){
        System.out.println(condition);
        PageResult<CourseOrder> result = courseOrderService.findPageResult(condition,page,limit);
        return result;
    }

    @PostMapping ("/add")
    @ResponseBody
    public String add(CourseOrder courseOrder){
        Map<String, Object> map = new HashMap<String, Object>();
        if (courseOrderService.save(courseOrder)>0){
            map.put("success",true);
            map.put("message","添加成功");
        }else {
            map.put("success",false);
            map.put("message","添加失败");
        }
        return JSON.toJSONString(map);
    }

    @PostMapping("/del")
    @ResponseBody
    public String deleteById(Integer id){
        Map<String, Object> map = new HashMap<String, Object>();
        if (courseOrderService.deleteById(id)>0){
            map.put("success",true);
            map.put("message","删除成功");
        }else {
            map.put("success",false);
            map.put("message","删除失败");
        }
        return JSON.toJSONString(map);
    }

    @PostMapping("/update")
    @ResponseBody
    public String updateBill(CourseOrder courseOrder){
        Map<String, Object> map = new HashMap<String, Object>();
        if (courseOrderService.update(courseOrder)>0){
            map.put("success",true);
            map.put("message","修改成功");
        }else {
            map.put("success",false);
            map.put("message","修改失败");
        }
        return JSON.toJSONString(map);
    }

}
